//
//  NetWorkingPerson.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/21.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkingPerson.h"

@implementation NetWorkingPerson

//获取个人信息详情
+(void)getUserInfoWithSuccessBlock:(void(^)(id responseObj))successBlock{
    
    [NetWorkTool requestTokenGetWithUrl:[NSString stringWithFormat:@"%@/%ld",getUserInfo,(long)[UserInfomationManager shareManager].userId] withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}

//获取用户中心信息
+(void)getUserHomeWithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestTokenGetWithUrl:[NSString stringWithFormat:@"%@",UserHome] withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}


//用户修改个人资料
+(void)updateUserInfoWithAvatarImg:(NSString *)avatarImgStr withName:(NSString *)name withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSDictionary *params = @{@"name":name,@"photo":avatarImgStr};
    [NetWorkTool requestTokenPostWithUrl:UserInfoUpdate WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}

///获取用户邀请记录
+(void)getUserRecordListWithParams:(NSDictionary *)params WithSuccessBlock:(void(^)(id responseObj))successBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager GET:UserRecordList parameters:params headers:[NetWorkTool getUserToken] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] == 500400) {
            [[XNProgressHUD shared] showErrorWithTitle:@"请重新登录"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Configuration loginClick];
            });
            return;
        }
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
    
}

///用户升级  UserUpgrade
+(void)userUpgradeWithParams:(NSDictionary *)params WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestTokenPostWithUrl:UserUpgrade WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

///用户提现  UserWithdraw
+(void)userWithDrawWithParams:(NSDictionary *)params WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestTokenPostWithUrl:UserWithdraw WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

///用户充值  ChargeOrder
+(void)chargeOrderWithParams:(NSDictionary *)params WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestTokenPostWithUrl:ChargeOrder WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}



@end





